﻿Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.IO
Imports System.Collections.Generic
Imports System.Text.RegularExpressions

Partial Public Class upload
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        Try
            Dim postedFile As HttpPostedFile = Request.Files("Filedata")

            Dim strDateFolder As String = Format(Now(), "\\yyyy\\MM\\dd\\")
            Dim strSaveFolder As String = Server.MapPath("EditorData") + "\image\SwfUpload" & strDateFolder
            'Kiểm tra sự tồn tại của thư mục
            If Not System.IO.Directory.Exists(strSaveFolder) Then
                'Tạo thư mục
                Call System.IO.Directory.CreateDirectory(strSaveFolder)
            End If

            Dim filename As String = GetUploadFileName(strSaveFolder, postedFile.FileName)
            postedFile.SaveAs(strSaveFolder & filename)
            ' write ra ten file de ben trang upload nhan duoc thong tin
            response.write(filename)
        Catch ex As Exception
        End Try


    End Sub

    'Lấy tên file + đường dẫn để save file
    Public Function GetUploadFileName(ByVal strPath As String, ByVal strFilename As String, Optional ByVal AddDatePath As Boolean = False) As String
        'Khai báo biến Regular Expression
        Dim MatchResults As System.Text.RegularExpressions.Match

        Dim strAdditionFolder As String = IIf(AddDatePath, Format(Now(), "\\yyyy\\MM\\dd\\"), "")
        Dim strSaveFile As String = strFilename
        Dim strSaveFolder As String = strPath & strAdditionFolder

        Try
            'Kiểm tra sự tồn tại của thư mục
            If Not System.IO.Directory.Exists(strSaveFolder) Then
                'Tạo thư mục
                Call System.IO.Directory.CreateDirectory(strSaveFolder)
            End If
            'Kiểm tra nếu file đã tồn tại thì tạo tên khác
            While System.IO.File.Exists(strSaveFolder & strSaveFile)
                MatchResults = System.Text.RegularExpressions.Regex.Match(strSaveFile, "(?<FileName>.*?)(?:\((?<AutoNumber>\d*?)\))?\.(?<FileType>\w*?)(?!.)")
                If MatchResults.Success Then
                    If MatchResults.Groups("AutoNumber").Value = vbNullString Then
                        strSaveFile = MatchResults.Groups("FileName").Value & "(1)." & MatchResults.Groups("FileType").Value
                    Else
                        strSaveFile = MatchResults.Groups("FileName").Value & "(" & (Integer.Parse(MatchResults.Groups("AutoNumber").Value) + 1).ToString & ")." & MatchResults.Groups("FileType").Value
                    End If
                End If
            End While
        Catch ex As Exception
            Throw ex
        End Try
        'tmpUploadFile.VirtualPath = strAdditionFolder.Replace("\", "/") & strSaveFile
        'tmpUploadFile.Filename = strSaveFile
        'tmpUploadFile.FullPath = strSaveFolder & strSaveFile
        Return strSaveFile
    End Function

End Class